<template>
  <div class="form-input" :class="{ '--is-tight': styleType == '2' }">
    <div class="tx-btn text-blue-dark mb-1" v-html="title"></div>
    <div class="form-input-group">
      <template v-for="(_, idx) in line" :key="`input-${idx}`">
        <input
          class="default-input"
          type="text"
          :placeholder="
            idx == 0
              ? placeholder
              : '请输入'
          "
          :value="myValue[idx]"
          @input="setValue(idx, $event)"
          :class="{
            'has-error': myError.length > 0 && idx == 0,
          }"
        />
        <div class="input-error" v-if="idx == 0">
          此为必填项
        </div>
      </template>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed, nextTick } from 'vue';

defineOptions({
  name: "FormInput"
});

interface Props {
  value?: string[];
  line?: number;
  title?: string;
  placeholder?: string;
  styleType?: string;
  optional?: boolean;
  componentKey?: string;
}

const props = withDefaults(defineProps<Props>(), {
  value: () => [],
  line: 1,
  title: "",
  placeholder: "",
  styleType: "",
  optional: false,
  componentKey: ""
});

interface Emits {
  (e: 'updateValue', value: string[]): void;
  (e: 'updateError', data: { key: string; isPass: boolean }): void;
}

const emit = defineEmits<Emits>();

const myError = ref<string[]>([]);

const myValue = computed({
  get() {
    return props.value || new Array(props.line);
  },
  set(nVal: string[]) {
    emit("updateValue", nVal);
    nextTick(checkError);
  },
});

const checkError = () => {
  if (props.optional) return;
  const isValid = myValue.value.some((v: string) => !!v);
  const key = props.componentKey ?? "tempKey";
  if (!isValid && myError.value.indexOf(key) < 0)
    myError.value.push(key);
  if (isValid && myError.value.indexOf(key) > -1)
    myError.value.splice(myError.value.indexOf(key), 1);

  emit("updateError", {
    key: props.componentKey,
    isPass: isValid,
  });
};

const setValue = (idx: number, e: Event) => {
  const target = e.target as HTMLInputElement;
  const value = JSON.parse(JSON.stringify(myValue.value));
  value.splice(idx, 1, target.value);
  myValue.value = value;
};
</script>

<style lang="scss" scoped>
.tx-btn {
  font-size: 14px;
  font-weight: 500;
  line-height: 1.285;
  letter-spacing: .04em
}

@media(max-width: 1023.98px) {
  .tx-btn {
      line-height:1.428
  }
}

@media(max-width: 767.98px) {
  .tx-btn {
      line-height:1.285
  }
}

.text-blue-dark {
  --bs-text-opacity: 1;
  color: rgba(0,50,160,var(--bs-text-opacity))!important;
}

.mb-1 {
  margin-bottom: 4px!important
}

input.default-input,textarea.default-textarea {
  font-size: 16px;
  font-weight: 400;
  line-height: 1.75;
  color: #1a1c20;
  background: none;
  background: #f7f7fa;
  border-radius: 4px;
  border: none;
  padding: 8px 12px;
  outline: none;
  width: 100%
}

@media(max-width: 1023.98px) {
  input.default-input,textarea.default-textarea {
      font-size:14px;
      line-height: 1.714;
      letter-spacing: .01em
  }
}

@media(max-width: 767.98px) {
  input.default-input,textarea.default-textarea {
      background:#fff
  }
}

input.default-input:focus,textarea.default-textarea:focus {
  padding: 7px 11px;
  border: 1px solid #0048e8
}

input.default-input::-moz-placeholder,textarea.default-textarea::-moz-placeholder {
  font-size: 16px;
  font-weight: 400;
  line-height: 1.75;
  color: #c0c0c4
}

input.default-input:-ms-input-placeholder,textarea.default-textarea:-ms-input-placeholder {
  font-size: 16px;
  font-weight: 400;
  line-height: 1.75;
  color: #c0c0c4
}

input.default-input::placeholder,textarea.default-textarea::placeholder {
  font-size: 16px;
  font-weight: 400;
  line-height: 1.75;
  color: #c0c0c4
}

@media(max-width: 1023.98px) {
  input.default-input::-moz-placeholder,textarea.default-textarea::-moz-placeholder {
      font-size:14px;
      line-height: 1.714;
      letter-spacing: .01em
  }

  input.default-input:-ms-input-placeholder,textarea.default-textarea:-ms-input-placeholder {
      font-size: 14px;
      line-height: 1.714;
      letter-spacing: .01em
  }

  input.default-input::placeholder,textarea.default-textarea::placeholder {
      font-size: 14px;
      line-height: 1.714;
      letter-spacing: .01em
  }
}

input.default-input.has-error,textarea.default-textarea.has-error {
  padding: 6px 10px;
  border: 2px solid #f04
}

input.default-input.has-error+.input-error,textarea.default-textarea.has-error+.input-error {
  opacity: 1;
  position: relative
}

input.default-input+.input-error,textarea.default-textarea+.input-error {
  position: absolute;
  margin-top: 4px;
  color: #ce031a;
  opacity: 0;
  transition: opacity .15s ease-in-out;
  pointer-events: none
}

.form-input {
  margin-bottom: 36px;
}

@media (max-width: 767.98px) {
  .form-input {
    margin-bottom: 24px;
  }
}

.form-input-group {
  display: flex;
  flex-direction: column;

  input:not(:last-child) {
    margin-bottom: 8px;
  }
}

.form-input.--is-tight {
  margin-bottom: 0;
}
</style>